<?php
/**
 * 请假功能模型
 */
class leaveModel extends RelationModel {

	//表单验证
	public $_validate	=	array(
		array('type','require','请选择请假类型',1,'',1),
		array('remark','require','请填写请假备注',1,'',1), 
		array('startTime','require','请选择开始时间',1,'',1),
		array('endTime','require','请选择结束时间',1,'',1), 
	);
    
    //发起关联，给后台使用，前台API 不需要使用这个
    protected $_link = array(
        'user' => array(
            'mapping_type' => BELONGS_TO,
            'class_name' => 'admin',
            'foreign_key' => 'userId',
            'as_fields'=>'storeId,userName,realName',
        ),
        'store' => array(
            'mapping_type' => BELONGS_TO,
            'class_name' => 'store',
            'foreign_key' => 'storeId',
            'as_fields'=>'name:storeName',
        ),        
    );    
	
	// --------------------------------------------------------------------
	
	/**
	 * 获得请假审核列表
	 * @param string $ids 用户id
	 * @return array
	 */
	public function getVerifyList($ids){
		$list		= array();
		$leaveType	= C('LEAVE_TYPE'); //请假类型
		$verifyType	= C('LEAVE_VERIFY_TYPE'); //审批状态类型
		$dateStart	= strtotime(date('Y-m', strtotime('last month'))) ;	//开始检查审核记录的那天（上个月的一号）
		$leave		= D('leave')->field('l.*,user.realName,store.name AS storeName')->alias('l')
						->join(C('DB_PREFIX')."admin AS user ON user.id = l.userId")
						->join(C('DB_PREFIX')."store AS store ON store.id = user.storeId")
						->where("l.createTime > {$dateStart} AND l.userId IN ({$ids})")->select();
		if($leave){
			foreach ($leave as $row){
				$temp = array();
				$temp['leaveId'] = $row['id'];
				$temp['realName'] = $row['realName'];
				$temp['storeName'] = $row['storeName'];
				$temp['leaveTypeName'] = $leaveType[$row['type']];
				$temp['leaveDays'] = abs((($row['startTime'] - $row['endTime']) / (3600 * 24))) + 1;
				$temp['leaveTimeLimit'] = date('Y-m-d', $row['startTime']) .'~'. date('Y-m-d', $row['endTime']);
				$temp['isVerify'] = $row['verifyType'] ? TRUE : FALSE;
				$temp['verifyTypeName'] = $row['verifyType'] ? $verifyType[$row['verifyType']] : "未审批";
				$list[] = $temp;
			}
		}
		return $list;
	}
	
	
	/**
	 * 获得请假记录详情
	 * @param int $leaveId
	 * @return  array
	 */
	public function getLeaveDetail($leaveId){
		$res		= array();
		$leaveType	= C('LEAVE_TYPE'); //请假类型
		$verifyType	= C('LEAVE_VERIFY_TYPE'); //审批状态类型
		$leave		= D('leave')->field('l.*,user.realName,store.name AS storeName')->alias('l')
						->join(C('DB_PREFIX')."admin AS user ON user.id = l.userId")
						->join(C('DB_PREFIX')."store AS store ON store.id = user.storeId")
						->where("l.id = {$leaveId}")->find();
						
		$res['leaveId']			= $leave['id'];
		$res['realName']		= $leave['realName'];
		$res['storeName']		= $leave['storeName'];
		$res['leaveTypeName']	= $leaveType[$leave['type']];
		$res['leaveDays']		= ((strtotime($leave['startTime']) - strtotime($leave['endTime'])) / (3600 * 24)) + 1;
		$res['leaveTimeLimit']	= date('Y-m-d', $leave['startTime']) .'~'. date('Y-m-d', $leave['endTime']);
		$res['leaveRemark']		= $leave['remark'];
		$res['isVerify']		= $leave['verifyType'] ? TRUE : FALSE;
		$res['verifyTypeName']	= $leave['verifyType'] ? $verifyType[$leave['verifyType']] : "未审批";
		
		return $res;
	}
	
	/**
	 * 获得假种选项
	 * 
	 * @return  array
	 */
	public function getLeaveOptions(){
		$leaveType	= C('LEAVE_TYPE'); //请假类型
		$list = array();
		foreach($leaveType as $key=>$val){
			$list[] = array('leaveTypeId'=>$key, 'leaveTypeName'=>$val);
		}
		return $list;
	}
	
	/**
	 * 请假审核选项
	 * 
	 * @return  array
	 */
	public function getVerifyOptions(){
		$verifyType	= C('LEAVE_VERIFY_TYPE'); //审批状态类型
		$list = array();
		foreach($verifyType as $key=>$val){
			$list[] = array('typeId'=>$key, 'typeName'=>$val);
		}
		return $list;
	}
}

/* End of file leaveModel.class.php */